package com.Controller;

import cn.hutool.core.util.StrUtil;
import com.Bean.Good;
import com.Bean.User;
import com.Common.CodeMsg;
import com.Common.Constant;
import com.Common.Result;
import com.Dao.GoodDao;
import com.Service.SealOrderService;
import com.Service.purchaseOrderService;
import com.Util.RespUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: hkw14
 * @time: 2021/5/6 20:49
 */
@WebServlet("/purchaseOrder.do")
public class purchaseOrderServlet extends HttpServlet {
    private static final long serialVersionUID = 1528719453359156761L;

    purchaseOrderService service = new purchaseOrderService();
    GoodDao goodDao = new GoodDao();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String type = req.getParameter("service");
        try {
            //利用反射调用方法
            this.getClass().getDeclaredMethod(type, HttpServletRequest.class,HttpServletResponse.class).invoke(this, req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String limit = req.getParameter("limit");
        String page = req.getParameter("page");
        String goodName = req.getParameter("goodName");
        String userName = req.getParameter("userName");
        Map<String,Object> params = new HashMap<String,Object>();

        if(StrUtil.isNotBlank(goodName)) {
            params.put("goodName", goodName);
        }
        if(StrUtil.isNotBlank(userName)) {
            params.put("userName", userName);
        }
        Result result =service.queryPage(params, page, limit);
        RespUtil.write(resp,result);
    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取所有要删除的用户ID
        User user = (User)req.getSession().getAttribute(Constant.CURRENT_USER);
        if (user.getRole() != 1){
            RespUtil.write(resp,new Result(CodeMsg.USER_RIGHTS_ERROR));
            return;
        }
        String[] ids = req.getParameterValues("ID");
        System.out.println(Arrays.toString(ids));
        Result rs = service.deleteOrder(ids);
        RespUtil.write(resp, rs);
    }

    protected void addOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = (User)req.getSession().getAttribute(Constant.CURRENT_USER);
        Integer goodID = Integer.valueOf(req.getParameter("goodID"));
        Integer number = Integer.valueOf(req.getParameter("number"));
        Good good= goodDao.getGoodByID(goodID);
        String price = req.getParameter("unitPrice");
//        System.out.println(goodID + number + price );
//        System.out.println(good);
        BigDecimal unitPrice = new BigDecimal(price);
        Result result = service.addOrder(goodID, user.getID(), good.getGoodName(), number, user.getUserName(), unitPrice);
        RespUtil.write(resp, result);
    }
}
